package com.smallrig.mall.template.cola;

import cn.hutool.json.JSONUtil;
import com.smallrig.extension.ext.ExtensionExecutor;
import com.smallrig.extension.statemachine.Action;
import com.smallrig.extension.statemachine.Condition;
import com.smallrig.extension.statemachine.StateMachine;
import com.smallrig.extension.statemachine.builder.StateMachineBuilder;
import com.smallrig.extension.statemachine.builder.StateMachineBuilderFactory;
import com.smallrig.mall.template.entity.Order;
import com.smallrig.mall.template.state.OrderStatus;
import com.smallrig.mall.template.state.OrderStatusChangeEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

/**
 * @description: 状态机配置
 * @author：lrk
 * @date: 2023/9/15
 */
@Configuration
@Slf4j
public class ColaStatemachineConfig {

    @Resource
    private ExtensionExecutor extensionExecutor;

    @Resource
    private WaitPayState waitPayState;

    @Resource
    private WaitDeliveryState waitDeliveryState;

    @Resource
    private WaitReceiveState waitReceiveState;

    @Bean
    public StateMachine<OrderStatus, OrderStatusChangeEvent, Order> colaOrderStateMachine() {
        String ORDER_STATE_MACHINE = "orderStateMachine";
        // 第一步：生成一个状态机builder
        StateMachineBuilder<OrderStatus, OrderStatusChangeEvent, Order> builder = StateMachineBuilderFactory.create();


        // 第二步：定义状态
        // 待支付状态->待发货状态 —— 支付
        builder.externalTransition() // 外部流转
                .from(OrderStatus.WAIT_PAYMENT)  // 起始状态
                .to(OrderStatus.WAIT_DELIVER)  // 目标状态
                .on(OrderStatusChangeEvent.PAYED)  // 事件
                .when(checkCondition()) // 流转需要校验的条件，校验不通过不会进行doAction
                .perform(waitPayState::pay);  //执行流转操作 这个action 我们可以按自己所需修改

        builder.externalTransition() // 外部流转
                .from(OrderStatus.WAIT_DELIVER)  // 起始状态
                .to(OrderStatus.WAIT_RECEIVE)  // 目标状态
                .on(OrderStatusChangeEvent.DELIVERY)  // 事件
                .when(checkCondition()) // 流转需要校验的条件，校验不通过不会进行doAction
                .perform(waitDeliveryState::delivery);  //执行流转操作 这个action 我们可以按自己所需修改

        builder.externalTransition() // 外部流转
                .from(OrderStatus.WAIT_RECEIVE)  // 起始状态
                .to(OrderStatus.FINISH)  // 目标状态
                .on(OrderStatusChangeEvent.RECEIVED)  // 事件
                .when(checkCondition()) // 流转需要校验的条件，校验不通过不会进行doAction
                .perform(waitReceiveState::receive);  //执行流转操作 这个action 我们可以按自己所需修改


        builder.internalTransition() // 内部流转
                .within(OrderStatus.WAIT_PAYMENT,OrderStatus.WAIT_DELIVER,OrderStatus.WAIT_RECEIVE)  // 起始状态
                .on(OrderStatusChangeEvent.UPDATE)  // 事件
                .when(checkCondition()) // 流转需要校验的条件，校验不通过不会进行doAction
                .perform(waitPayState::updateOrder);  //执行流转操作 这个action 我们可以按自己所需修改



        // 创建状态机
        StateMachine<OrderStatus, OrderStatusChangeEvent, Order> orderStateMachine = builder.build(ORDER_STATE_MACHINE);
        String uml = orderStateMachine.generatePlantUML();
        log.info("创建状态机,uml={}", uml);
        return orderStateMachine;
    }
	
    
    private Condition<Order> checkCondition() {
        return (ctx) -> {
            log.info("checkCondition:{}", JSONUtil.toJsonStr(ctx));
            return true;
        };
    }

}
